<template>
  <div class="tree-content">
    <a-tree
      :tree-data="treeData"
      :replaceFields="replaceFields"
      :selectedKeys="selectedTreeKeys"
      :expandedKeys="expandedTreeKeys"
      @select="selectNode"
      @expand="expandNode"
      ref="tree"
    >
      <a-icon slot="switcherIcon" type="down" />
    </a-tree>
  </div>
</template>
<script>
export default {
  name: "model-tree",
  data() {
    return {
      //   treeData: [],
      replaceFields: { children: "child", title: "name", key: "id" },
      selectNodes: [],
      // 选中节点
      selectedTreeKeys: [],
      //当前选中节点的节点名称
      selectedTitle: "",
      //当前选中节点的节点属性
      attrDatas: [],
      // 展开节点
      expandedTreeKeys: [],
      // 选中节点及子阶段ue4Id
      selectUe4ids: [],
      // 属性类型
      attrType: {
        img: "图片",
        file: "文件",
        text: "文本",
      },
      url: {
        list: "/bim/bimStructureInfo/getTreeList",
      },
    };
  },
  mounted() {
    this.initCharts();
    // // 这里模拟后台请求动态变化的数据，每2S改变一次数据
    // setInterval(this.changeOption, 2000);
  },
  methods: {
    //树==============================================================================
    // 树结构节点点击触发
    selectNodeHeightLight(selectUe4ids) {
      // 去除高亮
      if (this.ue4Viewer && Object.keys(this.ue4Viewer).length != 0) {
        this.ue4Viewer.unhighlightAll();
        this.selectNodes = selectUe4ids || [];
        this.ue4Viewer.highlight(this.selectNodes);
      }
    },

    expandNode(keys) {
      this.expandedTreeKeys = [];
      this.expandedTreeKeys = keys;
    },
    // 树结构节点选中事件
    selectNode(val, option) {
      this.selectedTreeKeys = val;
      if (!val || val.length == 0) {
        // 取消节点选中
        // this.$emit('select', this.selectKeys)
        this.selectNodeHeightLight(this.selectUe4ids);
        this.$emit("select", val, option);
        return;
      }
      let node = this.getNodeById(this.treeData, val[0]);
      if (!node || !node.id) {
        return this.$message.error("未找到选中节点!");
      }
      //设置节点标题和属性
      this.selectedTitle = node.name;
      this.attrDatas = node.sub || [];

      this.selectUe4ids = this.getNodeUe4Id([node]);
      // 高亮ue4Id
      this.selectNodeHeightLight(this.selectUe4ids);
      // 飞入模型位置
      if (val.length > 0) {
        this.ue4Viewer.flyto(node, {
          id: this.selectUe4ids[0],
          distance: this.distance,
        });
      } else this.ue4Viewer.flyto(node, { distance: this.distance });
      this.$emit("select", val, option);
    },

    // 返回节点所有子节点ue4Id
    getNodeUe4Id(nodes) {
      let list = [];
      for (let i in nodes) {
        let node = nodes[i];
        if (node.ue4Id) list.push(node.ue4Id);
        // 存在节点
        if (node.child && node.child.length > 0) {
          let childList = this.getNodeUe4Id(node.child);
          if (childList && childList.length > 0) {
            childList.forEach((element) => {
              list.push(element);
            });
          }
        }
      }
      return list;
    },

    // 根据ue4Id选中树节点
    selectedNodeByue4Id(ue4Id) {
      // 查询id
      let node = this.getIdByUe4Id(this.treeData, ue4Id);
      if (node && node.id) {
        // 选中
        this.selectedTreeKeys = [];
        this.selectedTreeKeys.push(node.id);
        // 展开指定节点
        this.expandedTreeKeys = [];
        let ids = node.path.split("@");
        for (let i in ids) {
          if (ids[i] && ids[i] != "") this.expandedTreeKeys.push(ids[i]);
        }
      }
      return node;
    },

    // 根据ue4Id查询节点
    // treeData  数据源
    getIdByUe4Id(treeData, ue4Id) {
      let currentNode = {};
      for (let i in treeData) {
        let node = treeData[i];
        if (node.ue4Id == ue4Id) currentNode = node;
        // 找到节点，直接返回
        if (currentNode && currentNode.id) return currentNode;
        // 存在节点
        if (node.child && node.child.length > 0) {
          //递归
          let childNode = this.getIdByUe4Id(node.child, ue4Id);
          if (childNode && childNode.id) {
            currentNode = childNode;
            return currentNode;
          }
        }
      }
    },

    // 根据节点id返回节点对象
    getNodeById(treeData, id) {
      let currentNode = {};
      for (let i in treeData) {
        let node = treeData[i];
        if (node.id == id) {
          currentNode = node;
        }
        // 找到节点，直接返回
        if (currentNode && currentNode.id) return currentNode;
        // 存在节点
        if (node.child && node.child.length > 0) {
          let childNode = this.getNodeById(node.child, id);
          if (childNode && childNode.id) {
            currentNode = childNode;
            return currentNode;
          }
        }
      }
    },
    // // 请求树节点信息
    // loadData() {
    //   this.loading = true;
    //   let params = {};

    //   let res = modeldatas;
    //   if (res.success) {
    //     this.treeData = res.result;
    //   } else {
    //     this.$message.warning(res.message);
    //   }
    // },
  },
  created() {
    // this.loadData();
  },
  mounted() {
    if (this.ue4Viewer && Object.keys(this.ue4Viewer).length != 0) {
      this.ue4Viewer.setSelectedNodeByue4Id(this.selectedNodeByue4Id);
    }
  },
  props: {
    treeData: {
      type: Array,
      default: () => new Array(),
    },
    //ue4场景对象
    ue4Viewer: {
      type: Object,
      default: () => {
        return {};
      },
    },
    //默认展开的keys
    expandedKeys: {
      type: Array,
      default: () => new Array(),
    },
    //默认选中keys
    selectedKeys: {
      type: Array,
      default: () => new Array(),
    },
    //ue4 focus参数
    focusParam: {
      type: Object,
      default: () => {},
    },
  },
  watch: {
    expandedKeys: function (val, oldVal) {
      this.expandedTreeKeys = val;
    },
    selectedKeys: function (val, oldVal) {
      this.selectedTreeKeys = val;
    },
  },
  computed: {
    //聚集距离
    distance() {
      let d = 0;
      if (this.focusParam && this.focusParam.distance)
        d = this.focusParam.distance;
      return d;
    },
  },
};
</script>

import 'ant-design-vue/dist/antd.less';
<style lang="less" scoped>
.tree-content {
  text-align: left;
  overflow-y: auto;
  height: auto;
  max-height: 100%;
  visibility: visible;
}
</style>
